package com.zretc.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zretc.dao.UsersDao;
import com.zretc.entity.Users;
import com.zretc.impl.UsersDaoImpl;
import com.zretc.tools.MD5Util;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
		String md5pwd = MD5Util.getEncodeByMd5(pwd);
		System.out.println(username);
		System.out.println(md5pwd);
		UsersDao usersDao = new UsersDaoImpl();	
		
		Users users = usersDao.login(username, md5pwd);
		
		if(users != null){
			
			request.getSession().setAttribute("users", users);
			
			request.getRequestDispatcher("chooseModel.jsp").forward(request, response);
		}else{
			
			request.setAttribute("info", "错误：用户名或密码错误！");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
